package org.spring.blog.service.impl;

import org.spring.blog.dao.SettingsDao;
import org.spring.blog.pojo.Setting;
import org.spring.blog.response.ResponseResult;
import org.spring.blog.response.ResponseState;
import org.spring.blog.service.IWebSizeInfoService;
import org.spring.blog.utils.RedisUtil;
import org.spring.blog.utils.SnowflakeIdWorker;
import org.spring.blog.utils.TextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
@Transactional
public class WebSizeInfoService implements IWebSizeInfoService {

    @Autowired
    RedisUtil redisUtil;

    @Autowired
    private SettingsDao settingsDao;

    @Autowired
    private SnowflakeIdWorker snowflakeIdWorker;

    /**
     * 增加一个访问量的方法
     * <p>
     * 用户访问页面时增加一个访问量到 redis 数据库
     * 用户获取总的访问量时将 redis 中的数据 添加到 mysql 持久层
     */
    @Override
    public void updateViewCount() {
        Object viewCount = redisUtil.get(constant.redis_web_size_view_count);

        if (viewCount == null) {
            Setting viewCountSetting = settingsDao.findOneByKey(constant.db_web_size_view_count);

            if (viewCountSetting == null) {
                viewCountSetting = initViewCountSetting();

                settingsDao.save(viewCountSetting);
            }

            redisUtil.set(constant.redis_web_size_view_count, viewCountSetting.getValue());
        } else {
            redisUtil.incr(constant.redis_web_size_view_count, 1);
        }
    }

    /**
     * 获取网站标题
     *
     * @return 自定义返回对象
     */
    @Override
    public ResponseResult getWebSizeTitle() {
        Setting oneByKey = settingsDao.findOneByKey(constant.web_size_title);

        return ResponseResult.success().setData(oneByKey);
    }

    /**
     * 更新网站标题
     *
     * @param _title 标题
     * @return 自定义返回对象
     */
    @Override
    public ResponseResult updateWebSizeTitle(String _title) {
        // check data
        if (TextUtil.isEmpty(_title)) {
            return ResponseResult.failed(ResponseState.input_data_is_empty_message, ResponseState.input_data_is_empty_code);
        }

        Setting oneByKey = settingsDao.findOneByKey(constant.web_size_title);

        // complete data
        if (oneByKey == null) {
            oneByKey = new Setting().setId(String.valueOf(snowflakeIdWorker.nextId()))
                    .setUpdateTime(new Date())
                    .setCreateTime(new Date())
                    .setKey(constant.web_size_title);
        }

        oneByKey.setValue(_title);

        // save data
        settingsDao.save(oneByKey);

        return ResponseResult.success();
    }

    /**
     * 获取网站描述
     *
     * @return 自定义返回对象
     */
    @Override
    public ResponseResult getSeoInfo() {
        Setting keywordsSetting = settingsDao.findOneByKey(constant.web_size_keywords);
        Setting descriptionSetting = settingsDao.findOneByKey(constant.web_size_description);

        if (keywordsSetting == null || descriptionSetting == null) {
            return ResponseResult.failed();
        }

        Map<String, String> result = new HashMap<>();

        result.put(descriptionSetting.getKey(), descriptionSetting.getValue());
        result.put(keywordsSetting.getKey(), keywordsSetting.getValue());

        return ResponseResult.success().setData(result);
    }

    /**
     * 设置网站描述
     *
     * @param _keywords    关键字
     * @param _description 网站描述
     * @return 自定义返回对象
     */
    @Override
    public ResponseResult putSeoInfo(String _keywords, String _description) {
        // check data
        if (TextUtil.isEmpty(_keywords) && TextUtil.isEmpty(_description)) {
            return ResponseResult.failed(ResponseState.input_data_is_empty_message, ResponseState.input_data_is_empty_code);
        }

        // complete data
        Setting keywordsSetting = settingsDao.findOneByKey(constant.web_size_keywords);
        Setting descriptionSetting = settingsDao.findOneByKey(constant.web_size_description);

        if (keywordsSetting == null) {
            keywordsSetting = new Setting().setId(String.valueOf(snowflakeIdWorker.nextId()))
                    .setCreateTime(new Date())
                    .setUpdateTime(new Date())
                    .setKey(constant.web_size_keywords);
        }

        keywordsSetting.setValue(_keywords);

        if (descriptionSetting == null) {
            descriptionSetting = new Setting().setId(String.valueOf(snowflakeIdWorker.nextId()))
                    .setCreateTime(new Date())
                    .setUpdateTime(new Date())
                    .setKey(constant.web_size_description);
        }

        descriptionSetting.setValue(_description);

        // save data
        settingsDao.save(descriptionSetting);
        settingsDao.save(keywordsSetting);

        return ResponseResult.success();
    }

    /**
     * 获取网站浏览量
     *
     * @return 自定义返回对象
     */
    @Override
    public ResponseResult getWebSizeViewCount() {
        // check data
        String viewCountFRedis = (String) redisUtil.get(constant.redis_web_size_view_count);

        Setting viewCountSetting = settingsDao.findOneByKey(constant.db_web_size_view_count);

        // complete data
        // 数据为空则创建
        if (viewCountSetting == null) {
            viewCountSetting = initViewCountSetting();
        }

        // redis 为空则创建
        if (viewCountFRedis == null) {
            redisUtil.set(constant.redis_web_size_view_count, viewCountSetting.getValue());
        } else {
            // redis 不为空 则更新 数据库
            viewCountSetting.setValue(viewCountFRedis);
        }

        // change data
        settingsDao.save(viewCountSetting);

        // return data
        return ResponseResult.success().setData(viewCountSetting);
    }

    private Setting initViewCountSetting() {
        Setting viewCountSetting;
        viewCountSetting = new Setting().setId(String.valueOf(snowflakeIdWorker.nextId()))
                .setCreateTime(new Date())
                .setUpdateTime(new Date())
                .setKey(constant.db_web_size_view_count)
                .setValue("0");
        return viewCountSetting;
    }
}
